<?php 
/**
 * FCKeditor adapter for XOOPS
 *
 * @copyright	The XOOPS project http://xoopscube.org/
 * @license		http://www.fsf.org/copyleft/gpl.html GNU public license
 * @author		Taiwen Jiang (phppp or D.J.) <php_pp@hotmail.com>
 * @since		4.00
 * @version		$Id$
 * @package		xoopseditor
 */
if (!defined("XOOPS_ROOT_PATH")) {
    die("XOOPS root path not defined");
}

require_once XOOPS_ROOT_PATH."/class/xoopsform/formtextarea.php";

class XoopsFormFckeditor extends XoopsFormTextArea
{
	var $rootpath = "";
	var $_language = _LANGCODE;
	var $_upload = true;
	var $_width = "100%";
	var $_height = "500px";

    var $config = array();
    
	/**
	 * Constructor
	 *
     * @param	array   $configs  Editor Options
     * @param	binary 	$checkCompatible  true - return false on failure
	 */
	function XoopsFormFckeditor($configs, $checkCompatible = false)
	{
		$current_path = __FILE__;
		if ( DIRECTORY_SEPARATOR != "/" ) $current_path = str_replace( strpos( $current_path, "\\\\", 2 ) ? "\\\\" : DIRECTORY_SEPARATOR, "/", $current_path);
		$this->rootpath = substr(dirname($current_path), strlen(XOOPS_ROOT_PATH));
		
		if(is_array($configs)) {
			$vars = array_keys(get_object_vars($this));
			foreach($configs as $key => $val){
				if(in_array("_".$key, $vars)) {
					$this->{"_".$key} = $val;
				}else{
					$this->config[$key] = $val;
				}
			}
		}
		
		if($checkCompatible && !$this->isCompatible()) {
			return false;
		}
		
		$this->XoopsFormTextArea("", @$this->_name, @$this->_value);
		parent::setExtra("style='width: ".$this->_width."; height: ".$this->_height.";'");
	}

	/**
	 * get language
	 *
     * @return	string
	 */
	function getLanguage()
	{
		if(defined("_XOOPS_EDITOR_FCKEDITOR_LANGUAGE")) {
			$language = strtolower(constant("_XOOPS_EDITOR_FCKEDITOR_LANGUAGE"));
		}else{
			$language = str_replace('_','-',strtolower($this->_language));
		}
		
		return $language;
	}

	/**
	 * prepare HTML for output
	 *
     * @return	sting HTML
	 */
	function render()
	{
		global $myts;
		$ret = '';
		if ( @include_once(XOOPS_ROOT_PATH . $this->rootpath. "/fckeditor.php") )	{
			$oFCKeditor = new FCKeditor($this->getName());
			$oFCKeditor->BasePath	= XOOPS_URL.$this->rootpath. "/";
			$oFCKeditor->Width		= $this->_width;
			$oFCKeditor->Height		= $this->_height;
			$oFCKeditor->Value		= $this->_value;
			
			//$oFCKeditor->Config['BaseHref'] = XOOPS_URL.$this->rootpath. "/";
			if(is_readable(XOOPS_ROOT_PATH . $this->rootpath. '/editor/lang/'.$this->getLanguage().'.js')) {
				$oFCKeditor->Config['DefaultLanguage'] = $this->getLanguage();
			}
			
			if(defined("_XOOPS_EDITOR_FCKEDITOR_FONTLIST")){
				$oFCKeditor->Config['FontNames'] = _XOOPS_EDITOR_FCKEDITOR_FONTLIST;
			}
			
			if(!file_exists($config_file = XOOPS_ROOT_PATH."/cache/fckconfig.".$GLOBALS["xoopsModule"]->getVar("dirname", "n").".js")) {
				if ( $fp = fopen( $config_file , "wt" ) ) {
					$fp_content = "/* FCKconfig module configuration */\n";
					if(is_readable($config_mod = XOOPS_ROOT_PATH."/modules/".$GLOBALS["xoopsModule"]->getVar("dirname")."/fckeditor.config.js")) {
						$fp_content .= "/* Loaded from module local config file */\n".implode("", file($config_mod))."\n\n";
					}
					if(is_readable(XOOPS_ROOT_PATH."/modules/".$GLOBALS["xoopsModule"]->getVar("dirname")."/fckeditor.connector.php")) {
						$fp_content .= "var browser_path = FCKConfig.BasePath + 'filemanager/browser/default/browser.html?Connector=".XOOPS_URL."/modules/".$GLOBALS["xoopsModule"]->getVar("dirname", "n")."/fckeditor.connector.php';\n";
						$fp_content .= "FCKConfig.LinkBrowserURL = browser_path ;\n";
						$fp_content .= "FCKConfig.ImageBrowserURL = browser_path + '&Type=Image';\n";
						$fp_content .= "FCKConfig.FlashBrowserURL = browser_path + '&Type=Flash';\n\n";
					}
					if(is_readable(XOOPS_ROOT_PATH."/modules/".$GLOBALS["xoopsModule"]->getVar("dirname")."/fckeditor.upload.php")) {
						$fp_content .= "var uploader_path = '".XOOPS_URL."/modules/".$GLOBALS["xoopsModule"]->getVar("dirname", "n")."/fckeditor.upload.php';\n";
						$fp_content .= "FCKConfig.LinkUploadURL = uploader_path;\n";
						$fp_content .= "FCKConfig.ImageUploadURL = uploader_path + '?Type=Image';\n";
						$fp_content .= "FCKConfig.FlashUploadURL = uploader_path + '?Type=Flash';\n\n";
					}
					if(empty($this->_upload)) {
						$fp_content .= "FCKConfig.LinkUpload = false;\n";
						$fp_content .= "FCKConfig.ImageUpload = false;\n";
						$fp_content .= "FCKConfig.FlashUpload = false;\n\n";
					}
					
					fwrite( $fp, $fp_content );
					fclose( $fp );
				} else {
					xoops_error( "Cannot create fckeditor config file" );
				}
			}
			
			if(is_readable($config_file = XOOPS_ROOT_PATH."/cache/fckconfig.".$GLOBALS["xoopsModule"]->getVar("dirname").".js")) {
				$oFCKeditor->Config['CustomConfigurationsPath'] = XOOPS_URL . "/cache/fckconfig.".$GLOBALS["xoopsModule"]->getVar("dirname", "n").".js";
			}
			
			foreach($this->config as $key => $val) {
				$oFCKeditor->Config[$key] = $val;
			}
			
			//$oFCKeditor->SetVar('ToolbarSet', "Basic");
			$ret = $oFCKeditor->CreateHtml();
		}
		return $ret;
	}

	/**
	 * Check if compatible
	 *
     * @return
	 */
	function isCompatible()
	{
		if ( !is_readable(XOOPS_ROOT_PATH . $this->rootpath. "/fckeditor.php")) return false;
		include_once(XOOPS_ROOT_PATH . $this->rootpath. "/fckeditor.php");
		return FCKeditor::IsCompatible();
	}
}
?>